1 package uk.ac.roe.antigen.dialogs;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.util.ArrayList;
8 import java.util.Hashtable;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Vector;
12
13 import javax.swing.BorderFactory;
14 import javax.swing.BoxLayout;
15 import javax.swing.ImageIcon;
16 import javax.swing.JButton;
17 import javax.swing.JCheckBox;
18 import javax.swing.JLabel;
19 import javax.swing.JPanel;
20 import javax.swing.JScrollPane;
21 import javax.swing.border.BevelBorder;
22 import javax.swing.border.TitledBorder;
23
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.Target;
26
27 import uk.ac.roe.antigen.builder.TargetChooser;
28 import uk.ac.roe.antigen.utils.Config;
29
30 public class TargetChoiceFrame extends AntigenFrame implements TargetChooser {
31
32 private JLabel jLabel1;
33
34 private JPanel optionsPanel;
35
36 /***
37 * Auto-generated main method to display this JDialog
38 */
39 public static void main(String[] args) {
40 Config.load("/config.properties");
41 Config.setProperty(ANTIGEN_TARGETS,"foo,bar,not,eek");
42 Config.setProperty(ANTIGEN_TARGET_+"foo.selected","true");
43 Config.setProperty(ANTIGEN_TARGET_+"bar.selected","false");
44 Config.setProperty(ANTIGEN_TARGET_+"not.selected","true");
45
46 Project project2 = new Project();
47 Target target = new Target();
48 target.setName("foo");
49 target.setDescription("Foos description");
50 project2.addTarget(target);
51
52 target = new Target();
53 target.setName("bar");
54 project2.addTarget(target);
55 target = new Target();
56 target.setName("eek");
57 project2.addTarget(target);
58
59 TargetChooser inst = new TargetChoiceFrame(project2);
60
61 List targets = inst.showAndWaitForResponse();
62 System.out.println("Selected tasks:");
63 Iterator it = targets.iterator();
64 while ( it.hasNext()) {
65 String taskName=(String) it.next();
66 System.out.println(taskName);
67 }
68 }
69
70
71 private Project project;
72
73 public TargetChoiceFrame(Project project) {
74 super();
75 this.project=project;
76 setUpTasks();
77 initGUI2();
78 }
79
80 /***
81 * Parse the list of targets and set up the checkboxes accordingly
82 */
83 private void setUpTasks() {
84 String targetString = Config.getProperty(ANTIGEN_TARGETS);
85 logger.fine("Found targets: "+targetString);
86 if (targetString==null) {
87 return;
88 }
89 Hashtable projectTargets = project.getTargets();
90 String[] targets = targetString.split(",");
91 logger.fine("Split into "+targets.length);
92 for (int i = 0; i<targets.length;++i) {
93 String targetName = targets[i];
94 Target target = (Target) projectTargets.get(targetName);
95 if (target!=null) {
96 String description = target.getDescription();
97 String selectedOpt = Config.getProperty(ANTIGEN_TARGET_+targetName+".selected");
98 boolean selected = true;
99 if ("false".equals(selectedOpt)) {
100 selected = false;
101 }
102 taskDescs.add(new TaskDescription(targetName, description, selected));
103 logger.fine("Target "+ targetName +" found ("+description+"). Is selected initially? "+selected);
104 } else {
105 logger.fine("Target "+targetName+" not found in project, skipping");
106 }
107 }
108
109 }
110
111 private void initGUI2() {
112 try {
113
114 workPanel.setLayout(new BorderLayout());
115 workPanel.setBackground(Color.white);
116 String borderTitle = Config.getProperty(
117 "antigen.dialog.bordertitle", "antigen");
118 workPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
119 .createEtchedBorder(BevelBorder.RAISED, null, null),
120 borderTitle, TitledBorder.LEADING, TitledBorder.TOP,
121 new java.awt.Font("Eurostile", 0, 12), new java.awt.Color(
122 0, 0, 255)));
123
124 jLabel1 = new JLabel();
125 jLabel1.setText("Please select the tasks you wish to execute:");
126 workPanel.add(jLabel1, BorderLayout.NORTH);
127
128 optionsPanel = new JPanel();
129 JScrollPane scroller = new JScrollPane();
130 scroller.setAutoscrolls(true);
131 scroller.setOpaque(false);
132 scroller.setViewportView(optionsPanel);
133 workPanel.add(scroller, BorderLayout.CENTER);
134
135 optionsPanel.setOpaque(false);
136 optionsPanel
137 .setLayout(new BoxLayout(optionsPanel, BoxLayout.Y_AXIS));
138 Iterator it = taskDescs.iterator();
139 while (it.hasNext()) {
140 TaskDescription description = (TaskDescription) it.next();
141 JCheckBox checkBox = description.getCheckBox();
142 checkBox.setOpaque(false);
143 optionsPanel.add(checkBox);
144
145 }
146 {
147 JPanel buttons2 = new JPanel();
148 southPanel.add(buttons2,BorderLayout.SOUTH);
149 buttons2.setOpaque(false);
150 {
151 JButton jButton1 = new JButton();
152 buttons2.add(jButton1);
153 jButton1.setText("Next");
154 jButton1.setIcon(new ImageIcon(getClass().getClassLoader()
155 .getResource("img/rightarrow.png")));
156 jButton1.addActionListener(new ActionListener() {
157 public void actionPerformed(ActionEvent evt) {
158 OKpressed = true;
159 }
160 });
161 }
162 }
163 } catch (Exception e) {
164 e.printStackTrace();
165 }
166 }
167
168 private List taskDescs = new ArrayList();
169 private class TaskDescription {
170 private String name;
171
172 private String description;
173
174 private JCheckBox checkBox;
175
176 public TaskDescription(String name, String description, boolean defaultEnabled) {
177 this.name = name;
178 this.description = description;
179 assert name !=null;
180 String label = description !=null ? description : name ;
181 this.checkBox = new JCheckBox(label,defaultEnabled);
182 }
183 /***
184 * @return Returns the checkBox.
185 */
186 public JCheckBox getCheckBox() {
187 return checkBox;
188 }
189 /***
190 * @return Returns the description.
191 */
192 public String getDescription() {
193 return description;
194 }
195 /***
196 * @return Returns the name.
197 */
198 public String getName() {
199 return name;
200 }
201 }
202
203 boolean OKpressed = false;
204
205 /***
206 * Display the dialog and return an array of the selected options
207 *
208 * @return
209 */
210 public Vector showAndWaitForResponse() {
211 show();
212 while (!OKpressed) {
213 try {
214 Thread.sleep(200);
215 } catch (InterruptedException ie) {
216 }
217 }
218 dispose();
219 Vector theChosen = new Vector();
220 Iterator it = taskDescs.iterator();
221 while (it.hasNext()) {
222 TaskDescription description = (TaskDescription) it.next();
223 JCheckBox box = description.getCheckBox();
224 if (box.isSelected()) {
225 theChosen.add(description.getName());
226 }
227 }
228 return theChosen;
229 }
230
231
232
233
234
235
236 public void shutDown() {
237 logger.info("TargetChoiceFrame closing");
238
239 }
240
241 }